package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.UserInfo;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

public class AddContactActivity extends BaseActivity {

    private EaseTitleBar titleBar;
    private EditText search;
    private LinearLayout userList;
    private Button addBtn;
    private TextView userText;
    private UserInfo userInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);
        initView();
        initLienter();
    }
    private void initView() {
        userList = findViewById(R.id.userList);
        titleBar = findViewById(R.id.title_bar);
        search = findViewById(R.id.add_search_et);
        addBtn = findViewById(R.id.add_user_btn);
        userText = findViewById(R.id.search_add_user_text);

    }
    private void initLienter() {

        titleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                finish();
            }
        });

        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                   searchUser();
                }
                return false;
            }
        });
        //添加好友
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addUser();
            }
        });
    }

        private void searchUser(){
            String userId = search.getText().toString().trim();
            if(TextUtils.isEmpty(userId)){
                showToast("请输入用户名");
                return;
            }
            //去服务查找用户是否存在
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {

                @Override
                public void run() {
                    userInfo = new UserInfo(userId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            userList.setVisibility(View.VISIBLE);
                            userText.setText(userInfo.getName());
                        }
                    });
                }
            });

        }

        private void addUser(){
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().addContact(userInfo.getName(),"添加好友");
                        showToast("发送好友邀请成功!");
                    } catch (HyphenateException e) {
                        showToast("发送好友邀请失败!");
                        e.printStackTrace();

                    }
                }
            });
        }

}